package com.sy.sevice.impl;

import com.sy.constant.BusinessConstant;
import com.sy.dto.JsonResult;
import com.sy.entity.Student;
import com.sy.entity.User;
import com.sy.mapper.AdminMapper;
import com.sy.mapper.StudentMapper;
import com.sy.mapper.TeacherMapper;
import com.sy.sevice.UserService;
import com.sy.vo.LoginUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Monkey-D-Gu
 * @version 1.0
 * @date 2020/9/11 0011 11:26
 * @usage 用户类相关的服务层实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 学生登录
     * @param loginUserVo 登录信息
     * @return JsonResult对象
     */
    @Override
    public JsonResult loginThroughStudent(LoginUserVo loginUserVo) {
        String username = loginUserVo.getUsername();
        String password = loginUserVo.getPassword();
        //用户名
        //1.判空
        if (StringUtils.isBlank(username)) {
            return JsonResult.buildFailure(400,"用户名不能为空");
        }
        //存在
        User user = studentMapper.getUserByUsername(username);
        if (null == user) {
            return JsonResult.buildFailure(400,"用户名不存在");
        }
        //密码
        //1.判空
        if (StringUtils.isBlank(password)) {
            return JsonResult.buildFailure(400,"密码不能为空");
        }
        //2.正确
        if (!StringUtils.equals(user.getPassword(),password)) {
            return JsonResult.buildFailure(400,"密码不正确");
        }

        //校验完成，登陆成功
        //将密码置成******
        user.setPassword(BusinessConstant.HIDE_PASSWORD);
        //返回状态码200
        return JsonResult.buildSuccess(200,user);
    }

    /**
     * 老师登录
     * @param loginUserVo 登录信息
     * @return JsonResult对象
     */
    @Override
    public JsonResult loginThroughTeacher(LoginUserVo loginUserVo) {
        String username = loginUserVo.getUsername();
        String password = loginUserVo.getPassword();
        //用户名
        //1.判空
        if (StringUtils.isBlank(username)) {
            return JsonResult.buildFailure(400,"用户名不能为空");
        }
        //存在
        User user = teacherMapper.getUserByUsername(username);
        if (null == user) {
            return JsonResult.buildFailure(400,"用户名不存在");
        }
        //密码
        //1.判空
        if (StringUtils.isBlank(password)) {
            return JsonResult.buildFailure(400,"密码不能为空");
        }
        //2.正确
        if (!StringUtils.equals(user.getPassword(),password)) {
            return JsonResult.buildFailure(400,"密码不正确");
        }

        //校验完成，登陆成功
        //将密码置成******
        user.setPassword(BusinessConstant.HIDE_PASSWORD);
        //返回状态码200
        return JsonResult.buildSuccess(200,user);
    }

    /**
     * 管理员登录
     * @param loginUserVo 登录信息
     * @return JsonResult对象
     */
    @Override
    public JsonResult loginThroughAdmin(LoginUserVo loginUserVo) {
        String username = loginUserVo.getUsername();
        String password = loginUserVo.getPassword();
        //用户名
        //1.判空
        if (StringUtils.isBlank(username)) {
            return JsonResult.buildFailure(400,"用户名不能为空");
        }
        //存在
        User user = adminMapper.getUserByUsername(username);
        if (null == user) {
            return JsonResult.buildFailure(400,"用户名不存在");
        }
        //密码
        //1.判空
        if (StringUtils.isBlank(password)) {
            return JsonResult.buildFailure(400,"密码不能为空");
        }
        //2.正确
        if (!StringUtils.equals(user.getPassword(),password)) {
            return JsonResult.buildFailure(400,"密码不正确");
        }

        //校验完成，登陆成功
        //将密码置成******
        user.setPassword(BusinessConstant.HIDE_PASSWORD);
        //返回状态码200
        return JsonResult.buildSuccess(200,user);
    }
}
